package resilienceManager.qmul.gemom;
import java.sql.*; 
import java.lang.*;

/**
 * The basic class to connect the mysql data base
 * get the information from the DB 
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */


public class JDBCConnect {
	
	// the driver of mysql 
	private final String driver= "com.mysql.jdbc.Driver";
	// the url of database
	private final String url="jdbc:mysql://localhost:3306/test";
	// the username
	private final String user="root";
	// the password
	private final String password = "admin"; 
	
	/**
	 * the main information array 
	 * */
//	private routernode[] routers;
//	private publisher[] publishers;
//	private subscriber[] subscribers;
//	private Gbroker[] gbrokers;
	private namespace[] namespaces;
	private int[][] routings;
	
	/**
	 * get the gbroker array,
	 * @return gbrokers: the array of gbroker
	 
	Gbroker[] getGbrokerlist()
	{
		return gbrokers;
	}
	* */ 
	/** return the routing table
	 * @return routings: the planar array of the routing table
	 */
	int[][] getroutings()
	{
		return routings;
	}
	
	/**
	 * get the publisher array
	 * @return publishers: the array of the publisher class
	 
	publisher[] getPublisherlist()
	{
		return publishers;
	}
	* */
	/**
	 * get the router node array
	 * @return routers: the array of the router class
	 
	routernode[] getRouterlist()
	{
		return routers;
	}
	* */
	/**
	 * get the subscriber array
	 * @return 
	 
	subscriber[] getSubscriberlist()
	{
		return subscribers;
	}
	* */
	/**
	 * get the namespace array
	 * @return
	 * */
	namespace[] getNamespacelist()
	{
		return namespaces;
	}
	

	void setDB()
	{
		
		try { 
            // load the driver
            Class.forName(driver); 

            // connect with database
            Connection conn = DriverManager.getConnection(url, user, password); 

            if(!conn.isClosed()) 
            {
            	System.out.println("Succeeded connecting to the Database!");	
            }
              

            Statement statement = conn.createStatement();
            

            // get the information from gbroker table
           /* String sql = "select count(*) from gbrokers";
            
            ResultSet rs = statement.executeQuery(sql);
           
            int num_gbroker=0;
            
            if(rs.next()){
            	num_gbroker=rs.getInt(1);
            }
            gbrokers=new Gbroker[num_gbroker];
            
            sql = "select * from gbrokers";

            rs = statement.executeQuery(sql);
            
            int g_index=0;
            while(rs.next()) {
                
                int gid=rs.getInt("gid");
            	String ipaddress=rs.getString("ipa");
                int routerid=rs.getInt("nrouter");
                gbrokers[g_index]=new Gbroker(gid,routerid,ipaddress);   
                           
                g_index++;
               }
*/
            
            

            //get the information from namespace table
            String sql = "select count(*) from topiclist";
            
            ResultSet rs = statement.executeQuery(sql);
           
            int num=0;
            
            if(rs.next()){
            	num=rs.getInt(1);
            }
            
            namespaces=new namespace[num];
            
            sql="select * from topiclist";
            
            rs = statement.executeQuery(sql);
            
            int t_index=0;
            
            while(rs.next())
            {
            	 int tid=rs.getInt(1);
             	 String tc=rs.getString(2);
                 namespaces[t_index]=new namespace(tid,tc);   
                            
                 t_index++;
            }
            
            
            
            // close the connect with data base

               rs.close();
               conn.close(); 

              } catch(ClassNotFoundException e) { 

               System.out.println("Sorry,can`t find the Driver!"); 
               e.printStackTrace(); 

              } catch(SQLException e) { 

               e.printStackTrace(); 

              } catch(Exception e) { 

               e.printStackTrace(); 


              } 
		
	}
		

}
